#ifndef JSONUTIL
#define JSONUTIL

#include <iostream>
#include <string>
#include <sstream>
#include <memory>
#include <jsoncpp/json/json.h>

namespace bucai
{
    class jsonUtil
    {
    public:
        static bool Serialize(const Json::Value &root, std::string *str)
        {
            Json::StreamWriterBuilder swb;
            std::unique_ptr<Json::StreamWriter> ptr(swb.newStreamWriter());
            std::stringstream ss;
            ptr->write(root, &ss);
            *str = ss.str();
            return true;
        }
        static bool UnSerialize(const std::string &str, Json::Value *root)
        {
            Json::CharReaderBuilder crb;
            std::unique_ptr<Json::CharReader> ptr(crb.newCharReader());
            std::string serr;
            ptr->parse(str.c_str(), str.c_str() + str.size(), &(*root), &serr);

            if (serr.size() != 0)
            {
                std::cout << "UnSerialize err :> " << serr << std::endl;
                return false;
            }
            return true;
        }
    };
}

#endif